package com.tedu.element.map;

import java.util.Properties;

/**
 * 地图格子类型
 */
public enum TileType {
    // 枚举实例，构造函数提供默认值
    EMPTY(0, 2,0, true, true, false, "box", "空地"),
    ORANGEBLOCK(1,0,0,false,false, false, "box", "橙色积木"),
    REDBLOCK(2,1,0,false,false, false, "box", "红色积木"),
    GREENFLOOR(3,2,0,true,true, false, "box", "绿色地板(积木)"),
    WOODBOX(4,3,0,false,false, false, "box", "木箱"),
    BLUECONTAINER(5,0,1,false,false, false, "box", "深蓝色集装箱"),
    AZURECONTAINER(6,1,1,false,false, false, "box", "天蓝色集装箱"),
    BARREL(7,2,1,false,false, false, "box", "雪糕筒"),
    SIGNIRON(8,3,1,false,false, false, "box", "黄黑条纹铁块"),
    IRONFLOOR(9,0,2,true,true, false, "box", "铁地板"),
    PAPERBOX(10, 1, 2, false, false, false, "box", "纸箱"),
    TREE(11, 0, 0, false, true, true, "tree", "树"),
    BUSH(12, 0, 0, false, true, false, "bush", "草丛"),
    YELLOWHOUSE(13, 0, 0, false, true, true, "house", "黄色房子"),
    BLUEHOUSE(14, 1,0, false, true, true, "house", "蓝色房子");

    /**
     * 格子类型唯一标识码
     * 用于地图文件存储和网络传输等场景
     */
    private int code;

    private int dx;

    private int dy;

    /**
     * 是否可通行
     * true表示玩家可以移动到该格子
     * false表示该格子有障碍物阻挡
     */
    private boolean walkable;

    /**
     * 是否无敌/不可摧毁
     * true表示该格子不受炸弹影响(如空地)
     * false表示该格子可以被炸弹摧毁或影响
     */
    private boolean isInvincible;

    /**
     * 是否为高度为 2格的对象
     */
    private boolean tallObject;

    /**
     * 对应切片图片
     */
    private String imageKey;

    /**
     * 格子类型描述文本
     * 用于调试、日志输出和 UI显示
     */
    private String description;

    // 构造函数，用于设置默认值
    TileType(int code, int dx, int dy, boolean walkable, boolean isInvincible, boolean tallObject, String imageKey, String description) {
        this.code = code;
        this.dx = dx;
        this.dy = dy;
        this.walkable = walkable;
        this.isInvincible = isInvincible;
        this.tallObject = tallObject;
        this.imageKey = imageKey;
        this.description = description;
    }

    /**
     * 包级私有方法，用于从 Properties 对象初始化自身属性
     * @param pro 从配置文件加载的 Properties 对象
     */
    public void init(Properties pro) {
        String typeName = this.name();
        try {
            this.code = Integer.parseInt(pro.getProperty(typeName + ".code", String.valueOf(this.code)));
            this.dx = Integer.parseInt(pro.getProperty(typeName + ".dx", String.valueOf(this.dx)));
            this.dy = Integer.parseInt(pro.getProperty(typeName + ".dy", String.valueOf(this.dy)));
            this.walkable = Boolean.parseBoolean(pro.getProperty(typeName + ".walkable", String.valueOf(this.walkable)));
            this.isInvincible = Boolean.parseBoolean(pro.getProperty(typeName + ".invincible", String.valueOf(this.isInvincible)));
            this.tallObject = Boolean.parseBoolean(pro.getProperty(typeName + ".tall", String.valueOf(this.tallObject)));
            this.imageKey = pro.getProperty(typeName + ".imageKey", this.imageKey);
            this.description = pro.getProperty(typeName + ".description", this.description);
        } catch (Exception e) {
            System.err.println("警告: 解析类型 '" + typeName + "' 的属性时失败，将使用默认值。");
        }
    }

    public int getCode() {
        return code;
    }

    public int getDx() {
        return dx;
    }

    public int getDy() {
        return dy;
    }

    public boolean isWalkable() {
        return walkable;
    }

    public boolean isInvincible() {
        return isInvincible;
    }

    public boolean isTallObject() {
        return tallObject;
    }

    public String getImageKey() {
        return imageKey;
    }

    public String getDescription() {
        return description;
    }
}
